<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/10/24
 * Time: 6:17 PM
 */

namespace app\adminapi\logic\landing_page;


use app\common\logic\BaseLogic;
use app\common\model\landing_page\LandingPage;

class LandingPageLogic extends BaseLogic
{

    /**
     * 详情
     *
     * @return array
     */
    public static function detail($id)
    {
        return LandingPage::field('id, page_no, title, platform, promotion_link_id, admin_id, head_img, content, footer_img, next_button_content, read_button_content, create_time, update_time')
            ->append(['admin_name', 'platform_name'])
            ->where('id', $id)
            ->findOrEmpty()
            ->toArray();
    }


    /**
     * 更新落地页
     *
     * @param $params
     * @return bool
     */
    public static function save($params)
    {
        $id                = $params['id'] ?? '';
        $platform          = $params['platform'] ?? 0;
        $promotionLinkId   = $params['promotion_link_id'] ?? 0;
        $adminId           = $params['admin_id'] ?? 0;
        $title             = $params['title'] ?? '';
        $headImg           = $params['head_img'] ?? '';
        $content           = $params['content'] ?? '';
        $footerImg         = $params['footer_img'] ?? '';
        $nextButtonContent = $params['next_button_content'] ?? '';
        $readButtonContent = $params['read_button_content'] ?? '';

        if (!$promotionLinkId || !$content || !$headImg) {

            return false;
        }

        $landingPage = $id ? LandingPage::where('id', $id)->findOrEmpty() : new LandingPage();

        if (!$id) {
            $landingPage->page_no = self::getPageNo();
        }

        $landingPage->platform            = $platform;
        $landingPage->promotion_link_id   = $promotionLinkId;
        $landingPage->admin_id            = $adminId;
        $landingPage->title               = $title;
        $landingPage->head_img            = $headImg;
        $landingPage->content             = $content;
        $landingPage->footer_img          = $footerImg;
        $landingPage->next_button_content = $nextButtonContent;
        $landingPage->read_button_content = $readButtonContent;

        return $landingPage->save();
    }


    /**
     * 删除
     *
     * @return bool
     */
    public static function delete($id)
    {
        return LandingPage::destroy($id);
    }


    private static function getPageNo()
    {
        return substr(str_replace('.', '', uniqid()), -16);
    }


    /**
     * 查看落地页
     *
     * @param $pageNo
     * @return array
     */
    public static function show($pageNo)
    {
        if (!$pageNo) {

            return [];
        }

        $landingPage = LandingPage::where('page_no', $pageNo)
            ->field('id,promotion_link_id,page_no,title,head_img,content,footer_img,next_button_content,read_button_content')
            ->append(['hap_url'])
            ->findOrEmpty();
        if ($landingPage->isEmpty()) {

            return [];
        }

        return $landingPage->toArray();
    }
}
